package cn.t.repository.impl.user;

import cn.t.converter.user.ParticipantRoleConverter;
import cn.t.core.mode.YesNoEnum;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.core.enums.base.BaseStatusEnum;
import cn.t.model.user.ParticipantRoleDO;
import cn.t.persistence.entity.user.ParticipantRolePO;
import cn.t.persistence.mapper.user.ParticipantRoleMapper;
import cn.t.repository.user.ParticipantRoleRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 参与者和角色关联表数据仓库
 */
@Slf4j
@Repository
@AllArgsConstructor
public class ParticipantRoleRepositoryImpl extends RepositoryImpl<ParticipantRoleMapper, ParticipantRolePO, ParticipantRoleConverter, ParticipantRoleDO> implements ParticipantRoleRepository {


    @Override
    public List<ParticipantRoleDO> findByModel(ParticipantRoleDO participantRoleDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ? " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<ParticipantRolePO> list = getWrapper(participantRoleDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<ParticipantRolePO> getWrapper(ParticipantRoleDO participantRoleDO) {
        LambdaQueryChainWrapper<ParticipantRolePO> wrapper = lambdaQuery();

        if (!ObjectUtils.isEmpty(participantRoleDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(participantRoleDO.getGmtCreatedEnd())) {
            wrapper.between(ParticipantRolePO::getGmtCreated, participantRoleDO.getGmtCreatedStart(), participantRoleDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(participantRoleDO.getGmtModifiedStart()) && !ObjectUtils.isEmpty(participantRoleDO.getGmtModifiedEnd())) {
            wrapper.between(ParticipantRolePO::getGmtModified, participantRoleDO.getGmtModifiedStart(), participantRoleDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(participantRoleDO.getParticipantId())) {
            wrapper.eq(ParticipantRolePO::getParticipantId, participantRoleDO.getParticipantId());
        }
        if (!ObjectUtils.isEmpty(participantRoleDO.getRoleId())) {
            wrapper.eq(ParticipantRolePO::getRoleId, participantRoleDO.getRoleId());
        }
        if (!ObjectUtils.isEmpty(participantRoleDO.getUserId())) {
            wrapper.eq(ParticipantRolePO::getUserId, participantRoleDO.getUserId());
        }

        if (!ObjectUtils.isEmpty(participantRoleDO.getStatus())) {
            wrapper.eq(ParticipantRolePO::getStatus, participantRoleDO.getStatus());
        }else{
            wrapper.eq(ParticipantRolePO::getStatus, String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
        }

        if (!ObjectUtils.isEmpty(participantRoleDO.getIsDeleted())) {
            wrapper.eq(ParticipantRolePO::getIsDeleted, participantRoleDO.getIsDeleted());
        }else{
            wrapper.eq(ParticipantRolePO::getIsDeleted, YesNoEnum.FALSE.getCode());
        }

        wrapper.in(!CollectionUtils.isEmpty(participantRoleDO.getRoleIds()), ParticipantRolePO::getRoleId, participantRoleDO.getRoleIds());

        return wrapper;
    }

    @Override
    public long count(ParticipantRoleDO participantRoleDO) {
        return getWrapper(participantRoleDO).count();
    }

    @Override
    public boolean deleteByParticipantId(Long participantId) {
        return lambdaUpdate().set(ParticipantRolePO::getStatus, String.valueOf(BaseStatusEnum.DISABLE.getCode()))
                .set(ParticipantRolePO::getIsDeleted, String.valueOf(YesNoEnum.TRUE.getCode()))
                .set(ParticipantRolePO::getGmtModified, LocalDateTime.now()).eq(ParticipantRolePO::getParticipantId, participantId).update();
    }

    @Override
    public Boolean removeByParticipantId(Long participantId) {
        return getBaseMapper().removeByParticipantId(participantId);
    }

    @Override
    public List<ParticipantRoleDO> findMenuByParticipantId(Long participantId) {
        return getBaseMapper().findMenuByParticipantId(participantId);
    }

    @Override
    public List<ParticipantRoleDO> findMetalIssueByParticipantId(Long participantId) {
        return getBaseMapper().findMetalIssueByParticipantId(participantId);
    }
    @Override
    public List<String> findEmailAddressByParticipantId(Long participantId, Long firmId) {
        return getBaseMapper().findEmailAddressByParticipantId(participantId,firmId);
    }

    @Override
    public Boolean deleteByRoleId(Long roleId) {
        return getBaseMapper().deleteByRoleId(roleId);
    }
    @Override
    public Boolean deleteParticipantRoleByRoleId(Long roleId,Long firmId) {
        return getBaseMapper().deleteParticipantRoleByRoleId(roleId,firmId);
    }


    @Override
    public void disableRoleByUserId(Long userId) {
        lambdaUpdate().set(ParticipantRolePO::getStatus, String.valueOf(BaseStatusEnum.DISABLE.getCode()))
                .set(ParticipantRolePO::getIsDeleted, String.valueOf(YesNoEnum.FALSE.getCode()))
                .eq(ParticipantRolePO::getUserId, userId).update();
    }

    @Override
    public void enableRoleByUserId(Long userId) {
        lambdaUpdate().set(ParticipantRolePO::getStatus, String.valueOf(BaseStatusEnum.AVAILABLE.getCode()))
                .set(ParticipantRolePO::getIsDeleted, String.valueOf(YesNoEnum.FALSE.getCode()))
                .eq(ParticipantRolePO::getUserId, userId).update();
    }

    @Override
    public List<ParticipantRoleDO> findMetalReplenishByParticipantId(Long participantId) {
        return getBaseMapper().findMetalReplenishByParticipantId(participantId);
    }

    @Override
    public List<ParticipantRoleDO> findAllByParticipantIdIgnoreDisable(Long participantId) {
        return getBaseMapper().findAllByParticipantIdIgnoreDisable(participantId);
    }

    @Override
    public List<ParticipantRoleDO> findAllByParticipantId(Long participantId) {
        return getBaseMapper().findAllByParticipantId(participantId);
    }

    @Override
    public boolean removeById(Long id) {
        return getBaseMapper().removeById(id);
    }


}
